from moodlefuse.modules.assignments.parsers import AssignmentsParser, AssignmentFilesParser
from moodlefuse.modules.assignments.operations import SubmitAssignmentFile
from moodlefuse.modules.courses.parsers import CoursesParser
from moodlefuse.modules.resources.parsers import ResourcesParser
from moodlefuse.modules.forums.parsers import ForumsParser, DiscussionsParser, DiscussionParser

from mechanize import Browser, CookieJar
from moodlefuse.settings import MOODLE_LOGIN_URL, MOODLE_INDEX_URL

class API(object):
    def __init__(self, username, password):
        self.username = username
        self.password = password

        self.create_browser()

    def create_browser(self):
        self.browser = Browser()
        self.cookiejar = CookieJar()
        self.browser.set_cookiejar(self.cookiejar)

    def login(self):
        self.browser.open(MOODLE_LOGIN_URL)
        self.browser.select_form(predicate=lambda form: form.attrs.get('id') == 'login')
        self.browser.form.set_value(self.username, name='username')
        self.browser.form.set_value(self.password, name='password')
        resp = self.browser.submit()
        print 'Logging into account %s@%s ...' % (self.username, MOODLE_LOGIN_URL),
        if resp.geturl().endswith('/login/index.php'):
            print 'FAILED!'
            raise API.InvalidCredentials("Invalid username or password")

        print ' OK'

    def get_courses(self):
        parser = CoursesParser(self, self.browser, MOODLE_INDEX_URL)
        return parser.parse_courses()

    def get_resources(self, course_url):
        parser = ResourcesParser(self, self.browser, course_url)
        return parser.parse_resources()

    def get_assignments(self, course_url):
        parser = AssignmentsParser(self, self.browser, course_url)
        return parser.parse_assignments()

    def get_assignment_files(self, assignment_url):
        parser = AssignmentFilesParser(self, self.browser, assignment_url)
        return parser.parse_assignment_files()

    def get_assigment_information(self, assignment_url):
        pass

    def submit_assignment_file(self, file):
        print "submitting to %s file %s" % (file.assignment_url, file.filepath)

        operation = SubmitAssignmentFile(self.browser)
        operation.submit(file)

    def get_forums(self, course_url):
        parser = ForumsParser(self, self.browser, course_url)
        return parser.parse_forums()

    def get_discussions(self, forum_url):
        parser = DiscussionsParser(self, self.browser, forum_url)
        return parser.parse_discussions()

    def get_discussion(self, discussion_url):
        parser = DiscussionParser(self, self.browser, discussion_url)
        return parser.parse_discussion()

    def read_raw_file(self, name, url):
        print "loading file and url - %s - %s" % (name, url)
        filename, headers = self.browser.retrieve(url, filename=name)

        return int(headers['content-length'])

    class InvalidCredentials(Exception):
        def __init__(self, value):
            self.value = value
        def __str__(self):
            return repr(self.value)

    

  